package com.hd.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.hd.common.DataPage;
import com.hd.persistence.dao.CustomerDao;
import com.hd.persistence.dao.MachineInfoDao;
import com.hd.persistence.dao.PurchaseRecordDao;
import com.hd.persistence.model.Customer;
import com.hd.persistence.model.MachineInfo;
import com.hd.persistence.model.PurchaseRecord;
import com.hd.service.PurchaseManager;
import com.hd.utils.HdException;

public class PurchaseManagerImpl implements PurchaseManager {
	private CustomerDao customerDao;
	private MachineInfoDao machineInfoDao;
	private PurchaseRecordDao purchaseRecordDao;
	
	////////////////////客户档案////////////////////
	
	public void saveCustomer(Customer customer) throws HdException {
		try {
			customerDao.save(customer);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public void deleteCustomer(Long customerId) throws HdException {
		try {
			customerDao.removeById(customerId);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public Customer getCustomer(Long customerId) throws HdException {
		try {
			return customerDao.get(customerId);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public DataPage<Customer> pageQueryCustomers(int pageNo, int pageSize) throws HdException {
		try {
			return customerDao.pageQueryCustomers(pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public List<Customer> listCustomers() throws HdException{
		try{
			return customerDao.listCustomers();
		}catch(DataAccessException e){
			throw new HdException(e.getMessage());
		}
	}
	
	public DataPage<Customer> pageQueryCustomers(Long customerId, Date contractStartDate, Date contractEndDate, String machineModel, int pageNo, int pageSize) throws HdException {
		try{
			return customerDao.pageQueryCustomers(customerId, contractStartDate, contractEndDate, machineModel, pageNo, pageSize);
		}catch(DataAccessException e){
			throw new HdException(e.getMessage());
		}
	}
	
	////////////////////购买记录////////////////////
	
	public void savePurchaseRecord(PurchaseRecord purchaseRecord) throws HdException {
		try {
			purchaseRecordDao.savePurchaseRecord(purchaseRecord);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public void deletePurchaseRecord(Long purchaseRecordId) throws HdException {
		try {
			purchaseRecordDao.removeById(purchaseRecordId);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public PurchaseRecord getPurchaseRecord(Long purchaseRecordId) throws HdException {
		try {
			return purchaseRecordDao.get(purchaseRecordId);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public DataPage<PurchaseRecord> pageQueryPurchaseRecords(int pageNo, int pageSize) throws HdException {
		try {
			return purchaseRecordDao.pageQueryPurchaseRecords(pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public PurchaseRecord findPurchaseRecordBy(String machineNo) throws HdException{
		try {
			return purchaseRecordDao.findPurchaseRecordBy(machineNo);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public DataPage<PurchaseRecord> pageQueryPurchaseRecords(Long customerId, Date contractStartDate, Date contractEndDate, String machineModel, int pageNo, int pageSize) throws HdException {
		try {
			return purchaseRecordDao.pageQueryPurchaseRecords(customerId, contractStartDate, contractEndDate, machineModel, pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	////////////////////购买的机器信息////////////////////
	
	public void saveMachineInfo(MachineInfo machineInfo) throws HdException {
		try {
			machineInfoDao.save(machineInfo);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public void deleteMachineInfo(Long machineInfoId) throws HdException {
		try {
			machineInfoDao.removeById(machineInfoId);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public MachineInfo getMachineInfo(Long machineInfoId) throws HdException {
		try {
			return machineInfoDao.get(machineInfoId);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	public DataPage<MachineInfo> pageQueryMachineInfos(int pageNo, int pageSize) throws HdException {
		try {
			return machineInfoDao.pageQueryMachineInfos(pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new HdException(e.getMessage());
		}
	}
	
	////////////////////getter、setter////////////////////
	
	public CustomerDao getCustomerDao() {
		return customerDao;
	}
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	public MachineInfoDao getMachineInfoDao() {
		return machineInfoDao;
	}
	public void setMachineInfoDao(MachineInfoDao machineInfoDao) {
		this.machineInfoDao = machineInfoDao;
	}
	public PurchaseRecordDao getPurchaseRecordDao() {
		return purchaseRecordDao;
	}
	public void setPurchaseRecordDao(PurchaseRecordDao purchaseRecordDao) {
		this.purchaseRecordDao = purchaseRecordDao;
	}
	
}
